//
//  TXFailRequestModel.swift
//  Here_Swift
//
//  Created by 童煊 on 2018/1/9.
//  Copyright © 2018年 童煊. All rights reserved.
//

import UIKit

/** 失败任务当前状态*/
public enum TXFailRequestModelStatus:Int {
    case TXFailRequestModelStatusPaused //任务暂停待处理，默认初始状态
    case TXFailRequestModelStatusDone   //任务已完成
    case TXFailRequestModelStatusFailedAgain //任务再次失败
    case TXFailRequestModelStatusIgnored //不对任务做处理
    case TXFailRequestModelStatusProcessing //任务处理中
}

/** 处理失败任务策略*/
public enum TXFailRequestHandlePolicy:Int{
    case TXFailRequestHandlePolicyDefault //默认不做任何处理，暂时保留在管理类中
    case TXFailRequestHandlePolicyTryAgainOnce //再尝试请求一次
    case TXFailRequestHandlePolicyRepeat  //若请求失败则重复请求直到请求成功
    case TXFailRequestHandlePolicyIgnore  //忽略失败请求，设置为忽略会在下一次出现失败请求时被管理类清除
    case TXFailRequestHandlePolicyDisable //禁用失败请求，设置为禁用后将不可再被修改、操作，自动忽略请求
}

class TXFailRequestModel: NSObject {
    
    /** 请求路径*/
    private(set) var path : String?
    /** 请求参数*/
    private(set) var params : Dictionary<String, Any>?
    /** 请求类型*/
    private(set) var requestType : NetworkRequestType?
    /** 失败请求唯一Id*/
    private(set) var UID : String?
    
    /** 任务失败时携带的错误信息*/
    public var errorInfo : Any?    
    /** 请求成功回调*/
    var successClosure : TXResponseClosure? {
        get{
            return self.successClosure
        }
        set{
            self.successClosure = newValue
        }
    }
    /** 请求当前状态*/
    public var failRequestStatus : TXFailRequestModelStatus = .TXFailRequestModelStatusPaused
    /** 请求处理策略*/
    public var HandlePolicy : TXFailRequestHandlePolicy = .TXFailRequestHandlePolicyDefault
    
    
    /**
     创建失败请求模型
     */
    public class func failRequestModelWith(
        path:String,
        params:Dictionary<String, Any>?,
        requestType:NetworkRequestType,
        successClosure:@escaping TXResponseClosure)
        -> TXFailRequestModel
    {
        let failRequestModel = TXFailRequestModel.init()
        failRequestModel.path = path;
        failRequestModel.params = params;
        failRequestModel.requestType = requestType;
        failRequestModel.successClosure = successClosure;
        failRequestModel.UID = TXFailRequestModel.registFailRequestUID(failModel: failRequestModel)
        return failRequestModel;
    }
    
    
    /**
     创建模型ID
     */
     private class func registFailRequestUID(failModel:TXFailRequestModel) -> String {
        let date : Date = Date()
        let formatter = DateFormatter()
        formatter.dateFormat = "yyyy-MM-dd HH:mm:ss.SSS"
        formatter.locale = Locale.init(identifier: "en_US")
        let UID : String = failModel.path! + formatter.string(from: date)
        
        return UID
    }
    
    /**
     私有初始化方法 只允许使用类方法创建
     */
    private override init() {
        super.init()
    }
    
    /**
     重载描述信息
     */
    public override var description: String{
        let property : Array = [path!,params!,UID!,failRequestStatus,HandlePolicy] as [Any]
        let dic = NSObject.dictionaryWithValues(forKeys: property as! [String])
        
        return "\(dic)"
    }

}


